
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../entity/book_entity.dart';
import '../entity/inventory_result_entity.dart';
import '../event/inventory_event.dart';
import '../http/api.dart';
import '../main.dart';
import '../serial/send_helper.dart';
import '../util/analysis_book_rule.dart';
import '../util/toast_util.dart';

class Inventory extends StatefulWidget {
  const Inventory({Key? key}) : super(key: key);

  @override
  State<Inventory> createState() => _InventoryState();
}

class _InventoryState extends State<Inventory> {

  RxnString floor = RxnString();
  String? placeNo;
  RxInt total = RxInt(0);
  RxInt has = RxInt(0);
  RxInt borrowed = RxInt(0);
  RxInt lose = RxInt(0);
  RxInt wrong = RxInt(0);
  RxInt unKnow = RxInt(0);
  RxList<BookEntity> bookList = RxList();
  RxList<BookEntity> filterList = RxList();
  RxInt tab = RxInt(100);
  bool isRequesting = false;

  RxInt step = 1.obs;

  StreamSubscription? subscription;

  @override
  initState() {
    super.initState();
    subscription = eventFactory.on<InventoryEvent>().listen((event) {
      di.play('audio/di.wav');

      _analyze(event.data);
    });
    // uhfSerialPortHelper.sendData(start());

    player.play('audio/scan_place.mp3');
  }

  @override
  void dispose() {
    subscription?.cancel();
    subscription = null;
    super.dispose();
  }

  _analyze(String epc){
    if(placeNo == null){
      if(epc.startsWith('B0')){
        try{
          var length = int.parse(epc.substring(2,4));
          placeNo = epc.substring(4,4+length);
          _findPlace(placeNo);
          return;
        }catch(e){
          print(e);
        }
      }
    }else{
      if(!epc.startsWith('B0') && hasFinish){

        try{
          var no = analysisBookNoRule(epc);
          if(no == null || no.isEmpty){
            toastWarn('条码号解析失败');
            return;
          }
          var item = bookList.firstWhereOrNull((element) => element.no == no);
          if(item == null){
            bookList.add(BookEntity()..no = no);
            _findBook(no);
          }else{
            var index = bookList.indexOf(item);
            bookList.removeAt(index);
            item.hasRfid = true;
            item.result = _getResultStatus(item);
            bookList.insert(index, item);
            _calculate();
          }
        }catch(e){
          print(e);
        }
      }
    }
  }


  _findPlace(no) async {
    var name = await findPlace(no);
    if(name != null){
      floor.value = name;
      step.value = 2;
      player.play('audio/scan_book.mp3');
      _findRfidBooks(no);
    }else{
      placeNo = null;
    }
  }

  bool hasFinish = false;

  _findRfidBooks(no) async {
    var books = await findRfidPlaceBook(no);
    if(books!=null) {
      bookList.addAll(books);
      hasFinish = true;
      _calculate();
      _startGetStatus();

    }
  }

  //查询馆藏状态
  _startGetStatus(){
    var next = bookList.firstWhereOrNull((element) => element.status == -1);
    if(next != null){
      _getStatus(next);
    }
  }

  _getStatus(BookEntity book) async {
    if(isRequesting){
      return;
    }
    isRequesting = true;
    var result = await findBook(book.no);
    if(result.$1 == 429){
      isRequesting = false;
      Future.delayed(const Duration(seconds: 1),(){
        _startGetStatus();
      });
      return;
    }
    var index = bookList.indexOf(book);
    bookList.removeAt(index);
    if(result.$1 == 200 && result.$2 != null){
      if(result.$2.stateValue == 'in'){
        book.status = 0;
      }
      if(result.$2.stateValue == 'borrowed'){
        book.status = 1;
      }
    }else{
      book.status = -2;
    }
    book.result = _getResultStatus(book);
    bookList.insert(index, book);
    _calculate();
    isRequesting = false;
    _startGetStatus();
  }

  //最终状态
  _getResultStatus(BookEntity item){
    var result = 4;
    if(item.hasApi && item.hasRfid && placeNo == item.placeNo && (item.status == 0 || item.status == 1)){
      result = 0;
    }
    if(item.hasApi && !item.hasRfid && placeNo == item.placeNo && item.status == 1){
      result = 1;
    }
    if(item.hasApi && !item.hasRfid && placeNo == item.placeNo && item.status == 0){
      result = 2;
    }
    if(!item.hasApi && item.hasRfid && placeNo != item.placeNo && (item.status == 0 || item.status == 1)){
      result = 3;
    }
    return result;
  }

  _calculate(){
    total.value = bookList.length;
    has.value = bookList.where((e) => e.result == 0).length;
    borrowed.value = bookList.where((e) => e.result == 1).length;
    lose.value = bookList.where((e) => e.result == 2).length;
    wrong.value = bookList.where((e) => e.result == 3).length;
    unKnow.value = bookList.where((e) => e.result == 4).length;
  }



  _findBook(no) async {
    var book = await findRfidBook(no);
    if(book!=null && book.no != null) {
      book.hasRfid = true;
      bookList.removeWhere((element) => element.no == no);
      bookList.add(book);
      _calculate();
      _startGetStatus();
    }
  }

  _title(){
    return Container(
        margin: EdgeInsets.only(top: 12.h,bottom: 10.h),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset("assets/images/inventory_title.png",width: 60.w,fit: BoxFit.fitWidth,),
            Obx(() => Image.asset("assets/images/inventory_step$step.png",width: 180.w,fit: BoxFit.fitWidth,)),
            InkWell(
                onTap: (){
                  // uhfSerialPortHelper.sendData(stop());
                  Get.back();
                },
                child: Image.asset("assets/images/index.png",width: 60.w,fit: BoxFit.fitWidth,)
            )
          ],
        )
    );
  }


  _info(){
    return Container(
      width: double.infinity,
      height: 46.h,
      margin: EdgeInsets.only(bottom: 10.h),
      child: Obx(() => Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          InkWell(
            onTap: (){
              tab.value = 100;
            },
            child: Container(
              width: 90.w,
              height: 51.h,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/${tab.value == 100?'select_bg':'total_bg'}.png",
                  ),
                ),
              ),
              child:Text("本层应有图书数\n${total.value}",style: TextStyle(color: const Color.fromRGBO(41, 136, 255, 1),fontSize: 8.sp),textAlign: TextAlign.center,),
            ),
          ),
          InkWell(
            onTap: (){
              tab.value = 0;
            },
            child:  Container(
              width: 90.w,
              height: 51.h,
              alignment: Alignment.center,
              decoration:  BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/${tab.value == 0?'select_bg':'has_bg'}.png",
                  ),
                ),
              ),
              child:Text("本层在架图书数\n${has.value}",style: TextStyle(color: const Color.fromRGBO(24, 146, 255, 1),fontSize: 8.sp),textAlign: TextAlign.center,),
            ),
          ),
          InkWell(
            onTap: (){
              tab.value = 1;
            },
            child:    Container(
              width: 90.w,
              height: 51.h,
              alignment: Alignment.center,
              decoration:  BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/${tab.value == 1?'select_bg':'borrowed_bg'}.png",
                  ),
                ),
              ),
              child:Text("本层外借图书数\n${borrowed.value}",style: TextStyle(color: const Color.fromRGBO(20, 195, 195, 1),fontSize: 8.sp),textAlign: TextAlign.center,),
            ),
          ),
          InkWell(
            onTap: (){
              tab.value = 2;
            },
            child:  Container(
              width: 90.w,
              height: 51.h,
              alignment: Alignment.center,
              decoration:  BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/${tab.value == 2?'select_bg':'lose_bg'}.png",
                  ),
                ),
              ),
              child:Text("本层缺失图书数\n${lose.value}",style: TextStyle(color: const Color.fromRGBO(247, 33, 45, 1),fontSize: 8.sp),textAlign: TextAlign.center,),
            ),
          ),
          InkWell(
            onTap: (){
              tab.value = 3;
            },
            child:  Container(
              width: 90.w,
              height: 51.h,
              alignment: Alignment.center,
              decoration:  BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/${tab.value == 3?'select_bg':'wrong_bg'}.png",
                  ),
                ),
              ),
              child:Text("本层错架图书数\n${wrong.value}",style: TextStyle(color: const Color.fromRGBO(251, 82, 28, 1),fontSize: 8.sp),textAlign: TextAlign.center,),
            ),
          ),
          InkWell(
            onTap: (){
              tab.value = 4;
            },
            child: Container(
              width: 90.w,
              height: 51.h,
              alignment: Alignment.center,
              decoration:  BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/${tab.value == 4?'select_bg':'unKnow_bg'}.png",
                  ),
                ),
              ),
              child:Text("本层未知图书数\n${unKnow.value}",style: TextStyle(color: const Color.fromRGBO(102, 102, 102, 1),fontSize: 8.sp),textAlign: TextAlign.center,),
            ),
          )
        ],
      )),
    );
  }

  _scanFloorExample(){
    return SizedBox(
      height: 186.h,
      child: Stack(
        alignment: AlignmentDirectional.center,
        children: [
          Image.asset("assets/images/scan_floor.png",width: 240.w,fit: BoxFit.fitWidth,),
          Positioned(
            top: 70.h,
            right: 0,
            child: Image.asset("assets/images/gun.gif",width: 160.w,fit: BoxFit.fitWidth,),
          ),
          Positioned(
              top: 170.h,
              child: Text("请扫描层架标签",style: TextStyle(color: Colors.blue,fontSize: 12.sp,fontWeight: FontWeight.bold),)
          )

        ],
      )
    );
  }

  _scanBookExample(){
    return SizedBox(
      height: 186.h,
      child: Stack(
        alignment: AlignmentDirectional.center,
        children: [
          Image.asset("assets/images/scan_book.png",width: 240.w,fit: BoxFit.fitWidth,),
          Positioned(
            top: 70.h,
            right: 0,
            child: Image.asset("assets/images/gun.gif",width: 160.w,fit: BoxFit.fitWidth,),
          ),
          Positioned(
              top: 170.h,
              child: Text("请扫描层架上的图书",style: TextStyle(color: Colors.blue,fontSize: 12.sp,fontWeight: FontWeight.bold),)
          )
        ],
      )
    );
  }

  _label(){
    return Row(
      children: [
        _labelText('序号',60.w),
        _labelText('条码号',80.w),
        _labelText('书名',180.w),
        _labelText('索书号',80.w),
        _labelText('馆藏状态',40.w),
        _labelText('存放位置',100.w),
        _labelText('盘点状态',60.w),
      ],
    );
  }

  _labelText(text,width){
    return Container(
      width: width,
      height: 24.h,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        color: Color.fromRGBO(241, 245, 255, 1),
      ),
      child: Text(text,style: TextStyle(color: Colors.black,fontSize: 8.sp),),
    );
  }

  _valueText(text,width,{Color color = Colors.black}){
    return Container(
      width: width,
      height: 24.h,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
          color: Colors.white
      ),
      child: Text(text,style: TextStyle(color: color,fontSize: 8.sp),),
    );
  }

  _list(){
    return Obx(() => Container(
        height: 140.h,
        margin: EdgeInsets.only(bottom: 6.r),
        child: ListView.separated(
          itemBuilder: (context, index) {
            return _listItem(index);
          },
          itemCount: tab.value == 100?bookList.length:bookList.where((e) => e.result == tab.value).length,
          separatorBuilder: (BuildContext context, int index) {
            return const Divider(height: 0.5);
          },
        )
    ));
  }

  _listItem(index){
    var item = tab.value == 100?bookList[index]:bookList.where((e) => e.result == tab.value).toList()[index];
    return Row(
      children: [
        _valueText('${index+1}',60.w),
        _valueText('${item.no}',80.w),
        _valueText('${item.mainTitle}',180.w),
        _valueText('${item.searchNo}',80.w),
        _valueText('${item.stateValue?.replaceAll('in', '在馆').replaceAll('borrowed', '借出')}',40.w),
        _valueText('${item.placeName}',100.w),
        Container(
            width: 60.w,
            height: 24.h,
            alignment: Alignment.center,
            child: Text(item.result == 0?"在架" :item.result == 1?"外借" :item.result == 2?"缺失":item.result == 3?"错架":"未知"
              ,style:  TextStyle( fontSize:8.sp,color:
                item.result == 0?const Color.fromRGBO(24, 146, 255, 1) :
                item.result == 1?const Color.fromRGBO(20, 195, 195, 1):
                item.result == 2?const Color.fromRGBO(247, 33, 45, 1):
                item.result == 3?const Color.fromRGBO(251, 82, 28, 1):
                const Color.fromRGBO(102, 102, 102, 1),),
            )
        ),
      ],
    );
  }

  // _listItem(index){
  //   var item = tab.value == 100?bookList[index]:bookList.where((e) => e.result == tab.value).toList()[index];
  //   return Container(
  //     margin: EdgeInsets.only(top: 10.r),
  //     decoration: BoxDecoration(
  //       color: Colors.white,
  //       borderRadius: BorderRadius.all(Radius.circular(6.r)),
  //     ),
  //     padding: EdgeInsets.all(10.r),
  //     child: Column(
  //       children: [
  //         InkWell(
  //           onTap: (){
  //             var index = bookList.indexOf(item);
  //             bookList.removeAt(index);
  //             item.expand = !item.expand;
  //             bookList.insert(index, item);
  //           },
  //           child: Row(
  //             mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //             children: [
  //               SizedBox(width: 230.w,child:Text('${item.mainTitle}'),),
  //               Container(
  //                   width: 70.w,
  //                   height: 30.h,
  //                   margin: EdgeInsets.only(left: 10.r),
  //                   alignment: Alignment.center,
  //                   decoration: BoxDecoration(
  //                     color: item.result == 0?Colors.blue :
  //                     item.result == 1?const Color.fromARGB(255, 20, 196, 196) :
  //                     item.result == 2?Colors.red:
  //                     item.result == 3?const Color.fromARGB(255, 251, 82, 28):
  //                     Colors.grey,
  //                     borderRadius: BorderRadius.all(Radius.circular(6.r)),
  //                   ),
  //                   child: Text(item.result == 0?"在架" :item.result == 1?"外借" :item.result == 2?"缺失":item.result == 3?"错架":"未知"
  //                     ,style: const TextStyle(color: Colors.white),)
  //               ),
  //               Icon(item.expand?Icons.keyboard_arrow_up_rounded:Icons.keyboard_arrow_down_rounded,color: Colors.grey,)
  //             ],
  //           ),
  //         ),
  //         if(item.expand)...
  //           [
  //             Column(
  //               children: [
  //                 SizedBox(height: 6.h,),
  //                 Divider(height: 0.5.h, indent: 4.w, endIndent: 4.w, color: const Color.fromRGBO(86, 169, 255, 1),),
  //                 SizedBox(height: 6.h,),
  //                 Row(
  //                   children: [
  //                     const Text('存放位置：'),
  //                     Text('${item.placeName}',style: const TextStyle(color: Color.fromARGB(255, 20, 196, 196)),),
  //                   ],
  //                 ),
  //                 Row(
  //                   children: [
  //                     const Text('馆藏状态：'),
  //                     Text(item.status == -1?'待查询':item.status == -2?'未知':item.status == 0?"在馆":"外借",
  //                       style: TextStyle(color: item.status == -1?Colors.red:item.status == -2?const Color.fromARGB(255, 251, 82, 28):item.status == 0?Colors.blue:const Color.fromARGB(255, 20, 196, 196)),),
  //                   ],
  //                 ),
  //                 Row(
  //                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //                   children: [
  //                     Text('条码号：${item.no}'),
  //                     Text('索书号：${item.searchNo}'),
  //                   ],
  //                 )
  //               ],
  //             )
  //           ]
  //       ],
  //     )
  //   );
  // }


  _getTypeBooks(type){
    var list = [];
    bookList.where((e) => e.result == type).forEach((element) {
      list.add(InventoryResultEntity()
        ..no = element.no
        ..stateValue = element.stateValue
        ..placeNo = element.placeNo
      );
    });
    return list;
  }


  _action(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
          InkWell(
            onTap: () async {
              var result = await inventory(placeNo, _getTypeBooks(0), _getTypeBooks(1), _getTypeBooks(2), _getTypeBooks(3), _getTypeBooks(4));
              if(result){
                // uhfSerialPortHelper.sendData(stop());
                Get.back();
              }
            },
            child: Image.asset("assets/images/inventory_done.png",width: 100.w,fit: BoxFit.fitWidth,),
          ),
          SizedBox(width: 80.w,),
          InkWell(
            onTap: ()  async {
              var result = await inventory(placeNo, _getTypeBooks(0), _getTypeBooks(1), _getTypeBooks(2), _getTypeBooks(3), _getTypeBooks(4));
              if(result){
                floor.value = null;
                placeNo = null;
                total.value = 0;
                has.value = 0;
                borrowed.value = 0;
                lose.value = 0;
                wrong.value = 0;
                unKnow.value = 0;
                bookList.clear();
                tab.value = 100;
                isRequesting = false;
                hasFinish = false;
              }
              step.value = 1;
              player.play('audio/scan_place.mp3');

            },
            child: Image.asset("assets/images/inventory_next.png",width: 100.w,fit: BoxFit.fitWidth,),
          )
      ],
    );
  }

  _container(){
    return Container(
      width: 620.w,
      height: 255.h,
      padding: EdgeInsets.all(10.w),
      decoration:  BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(4.r)),
          color: Colors.white
      ),
      child: Column(
        children: [
          _floorInfo(),
          _label(),
          Obx(() => floor.value == null?_scanFloorExample():total.value == 0?_scanBookExample():_list()),
          Obx(() => Visibility(visible:total.value > 0,child: _action(),))
        ],
      ),
    );
  }

  _floorInfo(){
    return Container(
      width: double.infinity,
      height: 14.h,
      margin: EdgeInsets.only(bottom: 6.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            children: [
              Image.asset("assets/images/icon_floor.png",width: 8.w,fit: BoxFit.fitWidth,),
              Text(" 层架信息：",style: TextStyle(color: Colors.black,fontSize: 8.sp),),
              Obx(() => Text(floor.value??'',maxLines:2,style: TextStyle(color: Colors.black,fontSize: 8.sp),)),
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Container(
        width: double.infinity,
        height: double.infinity,
        padding: EdgeInsets.only(left: 10.r,right: 10.r),
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "assets/images/bg.png",
            ),
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _title(),
            _info(),
            _container(),
          ],
        )
      ),
    );
  }
}